using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace backend.Repositories
{
    public abstract class BaseRepo<TContext, TEntity>
    where TContext : DbContext
    where TEntity : class
    {
        protected readonly TContext Context;
        protected readonly DbSet<TEntity> DbSet;

        public BaseRepo(TContext context)
        {
            Context = context;
            DbSet = context.Set<TEntity>();
        }

        public async Task<TEntity> AddAsync(TEntity entity)
        {
            await DbSet.AddAsync(entity);
            return entity;
        }

        public void Delete(TEntity entity)
        {
            DbSet.Remove(entity);
        }

        public IQueryable<TEntity> GetAll()
        {
            return DbSet;
        }

        public TEntity? GetById(object id)
        {
            return DbSet.Find(id);
        }

        public void Update(TEntity entity)
        {
            Context.Entry(entity).State = EntityState.Modified;
        }
    }
}